package com.ruoyi.common.config;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.EmpConstants;
import com.ruoyi.common.core.redis.RedisCache;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Data
@Slf4j
public class EmpConfig {

    /**
     * 行政区域返回结果集数量
     */
    private static final String EMP_AREA_MAX_SIZE_KEY = "EMP_AREA_MAX_SIZE";

    @Autowired
    private RedisCache redisCache;

    private String getCacheKey(String configKey) {
        return CacheConstants.SYS_CONFIG_KEY + configKey;
    }

    public int getEmpAreaMaxSize() {
        String configKey = getCacheKey(EMP_AREA_MAX_SIZE_KEY);
        String configValue = redisCache.getCacheObject(configKey, String.class);

        log.info("mysql-EMP_AREA_MAX_SIZE:{}", configValue);

        //参数表没有设置最大数量:100
        if (StringUtils.isBlank(configValue)) {
            return EmpConstants.EMP_AREA_MAX_SIZE;
        }

        //是数字，则默认数据库已经配置的
        if (StringUtils.isNumeric(configValue)) {
            int areaMaxSize = Integer.parseInt(configValue);
            if (areaMaxSize > 0) {
                return areaMaxSize;
            }
        }

        //非数字，或者-1
        return EmpConstants.EMP_AREA_MAX_SIZE;
    }
}
